package cn.zcib.dao;

import cn.zcib.domain.Teacher;

/**
 * @Author Lucky赵
 * @Date 2021年08月11日 19:10
 */
public class TeacherDao {
    private static final Teacher[] teachers = new Teacher[5];
    static {
        Teacher tea1 = new Teacher("001", "张三", 23, "1998-02-28");
        Teacher tea2 = new Teacher("002", "李四", 22, "1999-03-12");
        Teacher tea3 = new Teacher("003", "王五", 20, "2001-05-20");
        teachers[0]=tea1;
        teachers[1]=tea2;
        teachers[2]=tea3;
    }

    public boolean addTea(Teacher teacher) {
        int index = -1;
        for (int i = 0; i < teachers.length; i++) {
            Teacher tea = teachers[i];
            if (tea == null) {
                index = i;
                break;
            }
        }
        if (index == -1)
            return false;
        else {
            teachers[index] = teacher;
            return true;
        }
    }

    public Teacher[] findAllTea() {
        return teachers;
    }

    public void delTeaById(String id) {
        //查找id在容器中的索引位置
        int index = getIndex(id);
        //将该索引位置使用null元素进行覆盖
        teachers[index] = null;
    }

    private int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < teachers.length; i++) {
            Teacher tea = teachers[i];
            if (tea != null && tea.getId().equals(id)) {
                index = i;
                break;
            }
        }
        return index;
    }

    public void updateStu(String id, Teacher teacher) {
        int index = getIndex(id);
        teachers[index] = teacher;
    }
}
